package com.kh.pajx.sop.view.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.kh.pajx.sop.base.ModelDrivenBaseAction;
import com.kh.pajx.sop.base.SOPConfig;
import com.kh.pajx.sop.domain.AreaMangerBack;
import com.kh.pajx.sop.domain.BalanceApp;
import com.kh.pajx.sop.domain.ForeignBack;
import com.kh.pajx.sop.domain.SysDepartment;
import com.kh.pajx.sop.domain.SysInsideUser;
import com.kh.pajx.sop.util.common.DwzAjaxResult;
import com.kh.pajx.sop.util.common.FormatDate;
import com.kh.pajx.sop.util.page.QueryHelper;
import com.kh.pajx.sop.util.sop.PropertyValidate;
import com.opensymphony.xwork2.ActionContext;

/**
 * 对外结算
 * 
 * @author: taller
 * @Createdate: 2014年9月22日 下午8:48:09
 */
@Controller
@Scope("prototype")
public class ForeignAction extends ModelDrivenBaseAction<BalanceApp> {

    private static final long serialVersionUID = -99321164236684887L;

    public String foreignSetUi() {
	Map<String, String> params = new HashMap<String, String>();
	params.put("userId", getCurrentUser().getUserId());
	params.put("appState", "0");
	List<ForeignBack> foreignList = foreignService.searchForeign(params);
	ActionContext.getContext().put("foreignList", foreignList);
	return "foreignSetUi";
    }

    public void addBalanceApp() {
	DwzAjaxResult dwz = new DwzAjaxResult();
	int billcode = balanceAppService.GetMaxID();
	if (billcode < 1) {
	    billcode = 10000000;
	} else {
	    billcode++;
	}
	String appDate = model.getAppDate();
	appDate = appDate.replaceAll("-", "");
	appDate = appDate.replaceAll(":", "");
	appDate = appDate.replaceAll(" ", "");
	appDate = appDate.substring(0, 12);
	model.setAppBillCode(billcode + "");
	model.setAppDate(appDate);
	model.setAppPerson(getCurrentUser().getUserId());
	model.setAppSumFee(model.getAppSumFee());
	model.setChkMoney(model.getAppSumFee());
	model.setAppType(model.getAppType());
	model.setBillState(model.getBillState());
	try {
	    if (model.getAppType().equals("2")) {// 行业经理申请状态修改
		dwz.setNavTabId("addAreaMangerUi");
		for (String str : dataList) {
		    if (str.indexOf(",") > 0) {
			String string[] = str.split(",");
			AreaMangerBack areaManger = areaMangerService.findAreaMangerById(string[0]);
			areaManger.setUuid(string[0]);
			areaManger.setAppBillCode(billcode + "");
			areaManger.setAppState("1");
			areaMangerService.updateAreaManger(areaManger);
		    } else {
			AreaMangerBack areaManger = areaMangerService.findAreaMangerById(str);
			areaManger.setUuid(str);
			areaManger.setAppBillCode(billcode + "");
			areaManger.setAppState("1");
			areaMangerService.updateAreaManger(areaManger);
		    }
		}
	    } else {
		dwz.setNavTabId("foreignSetUi");
		for (String str : dataList) {
		    if (str.indexOf(",") > 0) {
			String string[] = str.split(",");
			ForeignBack foreign = foreignService.findForeignById(string[0]);
			foreign.setUuid(string[0]);
			foreign.setAppBillCode(billcode + "");
			foreign.setAppState("1");
			foreignService.updateForeign(foreign);
		    } else {
			ForeignBack foreign = foreignService.findForeignById(str);
			foreign.setUuid(str);
			foreign.setAppBillCode(billcode + "");
			foreign.setAppState("1");
			foreignService.updateForeign(foreign);

		    }
		}
	    }
	    balanceAppService.addBalanceApp(model);
	    dwz.setMessage("申请成功");
	    dwz.setStatusCode("200");
	    DwzAjaxResult.josnMessage(dwz);
	} catch (Exception e) {
	    e.printStackTrace();
	    dwz.setMessage("申请失败");
	    dwz.setStatusCode("300");
	    DwzAjaxResult.josnMessage(dwz);
	}
    }

    // 添加行业经理申请
    public String addAreaMangerUi() {
	Map<String, String> params = new HashMap<String, String>();
	params.put("appState", "0");
	List<AreaMangerBack> areaMangerList = areaMangerService.searchAreaManger(params);
	ActionContext.getContext().put("areaMangerList", areaMangerList);
	return "addAreaMangerUi";
    }

    // 查看单据
    public String foreignList() {
    	QueryHelper qh = new QueryHelper(BalanceApp.class, "ba");
    	qh.addCondition("ba.appPerson =?", getCurrentUser().getUserId());
    	qh.addOrderProperty("ba.appBillCode", false);
    	qh.preparePageBean(balanceAppService, pageNum, numPerPage);
    	return "foreignList";
    }

    public void findForeignList() {// 申请单据查看
	StringBuffer htmlcode = new StringBuffer();
	Map<String, String> params = new HashMap<String, String>();
	params.put("appBillCode", foreign.getAppBillCode());
	List<ForeignBack> list = foreignService.searchForeign(params);
	if (list != null) {
	    for (int i = 0; i < list.size(); i++) {
		ForeignBack fb = list.get(i);
		htmlcode.append(fb.getUuid());
		htmlcode.append(",");
		htmlcode.append(fb.getAppBillCode());
		htmlcode.append(",");
		htmlcode.append(fb.getHsSclName());
		htmlcode.append(",");
		htmlcode.append(FormatDate.addTimeJoinFlag(fb.getYwEndDate()));
		htmlcode.append(",");
		htmlcode.append(fb.getYwPopSumNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwBvuserNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwPastBvuserNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwBaddNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwBuseRate());
		htmlcode.append(",");
		htmlcode.append(fb.getYwCustomCost());
		htmlcode.append(",");
		htmlcode.append(fb.getYwCustomPay());
		htmlcode.append(",");
		htmlcode.append(fb.getYwSchoolCost());
		htmlcode.append(",");
		htmlcode.append(fb.getYwSchoolPay());
		htmlcode.append(",");
		htmlcode.append(fb.getYwPhoneCost());
		htmlcode.append(",");
		htmlcode.append(fb.getYwPhonePay());
		htmlcode.append(",");
		htmlcode.append(fb.getYwSysoperCost());
		htmlcode.append(",");
		htmlcode.append(fb.getYwSysoperPay());
		htmlcode.append(",");
		htmlcode.append(fb.getDescribe() == null ? "" : fb.getDescribe());
		htmlcode.append(",");
		htmlcode.append(fb.getHsSclid());
		htmlcode.append(";");
	    }
	}
	HttpServletResponse response = ServletActionContext.getResponse();
	response.setHeader("Pragma", "No-Cache");
	response.setHeader("Cache-Control", "No-Cache");
	response.setDateHeader("Expires", 0);
	try {
	    response.setContentType("text/html");
	    response.setCharacterEncoding("utf-8");
	    response.getWriter().write(htmlcode.toString());
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }

    public void findAreaMangerList() {// 申请单据查看行业经理
	StringBuffer htmlcode = new StringBuffer();
	Map<String, String> params = new HashMap<String, String>();
	params.put("appBillCode", areaManger.getAppBillCode());
	List<AreaMangerBack> list = areaMangerService.searchAreaManger(params);
	if (list != null) {
	    for (int i = 0; i < list.size(); i++) {
		AreaMangerBack fb = list.get(i);
		htmlcode.append(fb.getUuid());
		htmlcode.append(",");
		htmlcode.append(fb.getAppBillCode());
		htmlcode.append(",");
		htmlcode.append(fb.getAreaName());
		htmlcode.append(",");
		htmlcode.append(FormatDate.addTimeJoinFlag(fb.getYwEndDate()));
		htmlcode.append(",");
		htmlcode.append(fb.getYwSchoolNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwPopSumNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwVuserNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwPastVuserNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwAddNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwUseRate());
		htmlcode.append(",");
		htmlcode.append(fb.getYwRoyalty());
		htmlcode.append(",");
		htmlcode.append(fb.getYwRoyaltyPay());
		htmlcode.append(",");
		htmlcode.append(fb.getDescribe() == null ? "" : fb.getDescribe());
		htmlcode.append(",");
		htmlcode.append(fb.getAreaCode());
		htmlcode.append(";");
	    }
	}
	HttpServletResponse response = ServletActionContext.getResponse();
	response.setHeader("Pragma", "No-Cache");
	response.setHeader("Cache-Control", "No-Cache");
	response.setDateHeader("Expires", 0);
	try {
	    response.setContentType("text/html");
	    response.setCharacterEncoding("utf-8");
	    response.getWriter().write(htmlcode.toString());
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }

    public void findForeignPrint() {// 申请单据打印预览
	StringBuffer htmlcode = new StringBuffer();
	Map<String, String> params = new HashMap<String, String>();
	params.put("appBillCode", foreign.getAppBillCode());
	List<ForeignBack> list = foreignService.searchForeign(params);
	if (list != null) {
	    for (int i = 0; i < list.size(); i++) {
		ForeignBack fb = list.get(i);
		htmlcode.append(fb.getHsSclName());
		htmlcode.append(",");
		htmlcode.append(FormatDate.addTimeJoinFlag(fb.getYwEndDate()));
		htmlcode.append(",");
		htmlcode.append(fb.getYwPopSumNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwBvuserNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwPastBvuserNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwBaddNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwBuseRate());
		htmlcode.append(",");
		htmlcode.append(fb.getYwCustomCost());
		htmlcode.append(",");
		htmlcode.append(fb.getYwCustomPay());
		htmlcode.append(",");
		htmlcode.append(fb.getYwSchoolCost());
		htmlcode.append(",");
		htmlcode.append(fb.getYwSchoolPay());
		htmlcode.append(",");
		htmlcode.append(fb.getYwPhoneCost());
		htmlcode.append(",");
		htmlcode.append(fb.getYwPhonePay());
		htmlcode.append(",");
		htmlcode.append(fb.getYwSysoperCost());
		htmlcode.append(",");
		htmlcode.append(fb.getYwSysoperPay());
		htmlcode.append(";");
	    }
	}
	HttpServletResponse response = ServletActionContext.getResponse();
	response.setHeader("Pragma", "No-Cache");
	response.setHeader("Cache-Control", "No-Cache");
	response.setDateHeader("Expires", 0);
	try {
	    response.setContentType("text/html");
	    response.setCharacterEncoding("utf-8");
	    response.getWriter().write(htmlcode.toString());
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }

    public void findAreaMangerPrint() {// 行业经理申请单据打印预览
	StringBuffer htmlcode = new StringBuffer();
	Map<String, String> params = new HashMap<String, String>();
	params.put("appBillCode", areaManger.getAppBillCode());
	List<AreaMangerBack> list = areaMangerService.searchAreaManger(params);
	if (list != null) {
	    for (int i = 0; i < list.size(); i++) {
		AreaMangerBack fb = list.get(i);
		htmlcode.append(fb.getAreaName());
		htmlcode.append(",");
		htmlcode.append(FormatDate.addTimeJoinFlag(fb.getYwEndDate()));
		htmlcode.append(",");
		htmlcode.append(fb.getYwSchoolNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwPopSumNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwVuserNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwPastVuserNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwAddNum());
		htmlcode.append(",");
		htmlcode.append(fb.getYwUseRate());
		htmlcode.append(",");
		htmlcode.append(fb.getYwRoyalty());
		htmlcode.append(",");
		htmlcode.append(fb.getYwRoyaltyPay());
		// htmlcode.append(",");
		// htmlcode.append(fb.getDescribe());
		htmlcode.append(";");
	    }
	}
	HttpServletResponse response = ServletActionContext.getResponse();
	response.setHeader("Pragma", "No-Cache");
	response.setHeader("Cache-Control", "No-Cache");
	response.setDateHeader("Expires", 0);
	try {
	    response.setContentType("text/html");
	    response.setCharacterEncoding("utf-8");
	    response.getWriter().write(htmlcode.toString());
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }

    public void updateBalanceApp() {// 撤销单据
	DwzAjaxResult dwz = new DwzAjaxResult();
	int state = 2;
	Map<String, String> params = new HashMap<String, String>();
	try {
	    BalanceApp app = balanceAppService.findBalanceAppById(model.getUuid());
	    params.put("appBillCode", app.getAppBillCode());
	    if (app.getBillState().equals("2")) {
		dwz.setNavTabId("appManageList");
		app.setUuid(model.getUuid());
		app.setBillState("1");
		app.setChkDate("");
		app.setChkPerson("");
		app.setPayDate("");
		app.setPayPerson("");
		if (app.getAppType().equals("1")) {
		    List<ForeignBack> list = foreignService.searchForeign(params);
		    if (list != null) {
			for (int i = 0; i < list.size(); i++) {
			    ForeignBack fb = list.get(i);
			    try {
				if (fb.getAppState().equals("2")) {
				    fb.setUuid(fb.getUuid());
				    fb.setAppState("1");
				    /*
				     * fb.setYwSysoperPay(fb.getYwSysoperCost());
				     * fb.setYwCustomPay(fb.getYwCustomCost());
				     * fb.setYwSchoolPay(fb.getYwSchoolCost());
				     * fb.setYwPhonePay(fb.getYwPhoneCost());
				     */
				    foreignService.updateForeign(fb);
				    state = 1;
				}
			    } catch (Exception e) {
				state = 2;
				e.printStackTrace();
			    }
			}
		    }
		} else if (app.getAppType().equals("2")) {
		    List<AreaMangerBack> list = areaMangerService.searchAreaManger(params);
		    if (list != null) {
			for (int i = 0; i < list.size(); i++) {
			    AreaMangerBack fb = list.get(i);
			    try {
				if (fb.getAppState().equals("2")) {
				    fb.setUuid(fb.getUuid());
				    fb.setAppState("1");
				    areaMangerService.updateAreaManger(fb);
				    state = 1;
				}
			    } catch (Exception e) {
				e.printStackTrace();
				state = 2;
			    }
			}
		    }
		}
	    } else if (app.getBillState().equals("3")) {
		dwz.setNavTabId("balanceAppPayManageList");
		app.setUuid(model.getUuid());
		app.setBillState("2");
		app.setPayDate("");
		app.setPayPerson("");
		if (app.getAppType().equals("1")) {
		    List<ForeignBack> list = foreignService.searchForeign(params);
		    if (list != null) {
			for (int i = 0; i < list.size(); i++) {
			    ForeignBack fb = list.get(i);
			    try {
				if (fb.getAppState().equals("3")) {
				    fb.setUuid(fb.getUuid());
				    fb.setAppState("2");
				    /*
				     * fb.setYwSysoperPay(fb.getYwSysoperCost());
				     * fb.setYwCustomPay(fb.getYwCustomCost());
				     * fb.setYwSchoolPay(fb.getYwSchoolCost());
				     * fb.setYwPhonePay(fb.getYwPhoneCost());
				     */
				    foreignService.updateForeign(fb);
				    state = 1;
				}
			    } catch (Exception e) {
				state = 2;
				e.printStackTrace();
			    }
			}
		    }
		} else if (app.getAppType().equals("2")) {
		    List<AreaMangerBack> list = areaMangerService.searchAreaManger(params);
		    if (list != null) {
			for (int i = 0; i < list.size(); i++) {
			    AreaMangerBack fb = list.get(i);
			    try {
				if (fb.getAppState().equals("3")) {
				    fb.setUuid(fb.getUuid());
				    fb.setAppState("2");
				    areaMangerService.updateAreaManger(fb);
				    state = 1;
				}
			    } catch (Exception e) {
				state = 2;
				e.printStackTrace();
			    }
			}
		    }
		}
	    }
	    balanceAppService.updateBalanceApp(app);
	    state = 1;
	} catch (Exception e) {
	    e.printStackTrace();
	    state = 2;
	}
	if (state == 1) {
	    dwz.setMessage("撤销成功");
	    dwz.setStatusCode("200");
	    DwzAjaxResult.josnMessage(dwz);
	} else {
	    dwz.setMessage("撤销失败");
	    dwz.setStatusCode("300");
	    DwzAjaxResult.josnMessage(dwz);
	}
    }

    public void addOneBalanceApp() {// 审批成功
	DwzAjaxResult dwz = new DwzAjaxResult();
	String chkDate = FormatDate.dateTimeDetail();
	chkDate = chkDate.substring(0, 12);
	String billState = model.getBillState();
	Double chkMoey = model.getChkMoney();
	Double chkMoey1 = 0.0;// 验证数据正确
	try {
	    model = balanceAppService.findBalanceAppById(model.getUuid());
	    model.setUuid(model.getUuid());
	    model.setChkDate(chkDate);
	    model.setChkPerson(getCurrentUser().getUserId());
	    model.setBillState(billState);
	    if (model.getAppType().equals("1")) {
		if (dataCost.length() > 0) {
		    Map<String, String> params = new HashMap<String, String>();
		    params.put("appBillCode", model.getAppBillCode());
		    List<ForeignBack> list = foreignService.searchForeign(params);
		    String[] data = dataCost.split(";");
		    if (list != null) {
			for (int i = 0; i < list.size(); i++) {
			    if (dataCost.indexOf(",") > 0) {
				String[] data1 = data[i].split(",");
				ForeignBack fb = list.get(i);
				fb.setUuid(fb.getUuid());
				fb.setAppState("2");
				fb.setYwCustomPay(Double.parseDouble(data1[0]));
				fb.setYwSchoolPay(Double.parseDouble(data1[1]));
				fb.setYwPhonePay(Double.parseDouble(data1[2]));
				fb.setYwSysoperPay(Double.parseDouble(data1[3]));
				fb.setDescribe(data1[4].equals("0.0") ? "" : data1[4]);
				foreignService.updateForeign(fb);
				chkMoey1 += Double.parseDouble(data1[0]) + Double.parseDouble(data1[1]) + Double.parseDouble(data1[2]) + Double.parseDouble(data1[3]);
			    }
			}
		    }
		    if (chkMoey != chkMoey1) {
			chkMoey = chkMoey1;
		    }
		    model.setChkMoney(chkMoey);
		} else {
		    model.setChkMoney(model.getAppSumFee());
		    Map<String, String> params = new HashMap<String, String>();
		    params.put("appBillCode", model.getAppBillCode());
		    List<ForeignBack> list = foreignService.searchForeign(params);
		    if (list != null) {
			for (int i = 0; i < list.size(); i++) {
			    ForeignBack fb = list.get(i);
			    fb.setUuid(fb.getUuid());
			    fb.setAppState("2");
			    fb.setYwCustomPay(fb.getYwCustomCost());
			    fb.setYwSchoolPay(fb.getYwSchoolCost());
			    fb.setYwPhonePay(fb.getYwPhoneCost());
			    fb.setYwSysoperPay(fb.getYwSysoperCost());
			    foreignService.updateForeign(fb);
			}
		    }
		}
	    } else {
		if (dataCost.length() > 0) {
		    Map<String, String> params = new HashMap<String, String>();
		    params.put("appBillCode", model.getAppBillCode());
		    List<AreaMangerBack> list = areaMangerService.searchAreaManger(params);
		    String[] data = dataCost.split(";");
		    if (list != null) {
			for (int i = 0; i < list.size(); i++) {
			    if (dataCost.indexOf(",") > 0) {
				String[] data1 = data[i].split(",");
				AreaMangerBack fb = list.get(i);
				fb.setUuid(fb.getUuid());
				fb.setAppState("2");
				fb.setYwRoyaltyPay(Double.parseDouble(data1[0]));
				fb.setDescribe(data1[1].equals("0.0") ? "" : data1[1]);
				areaMangerService.updateAreaManger(fb);
				chkMoey1 += Double.parseDouble(data1[0]);
			    }
			}
		    }
		    if (chkMoey != chkMoey1) {
			chkMoey = chkMoey1;
		    }
		    model.setChkMoney(chkMoey);
		} else {
		    model.setChkMoney(model.getAppSumFee());
		    Map<String, String> params = new HashMap<String, String>();
		    params.put("appBillCode", model.getAppBillCode());
		    List<AreaMangerBack> list = areaMangerService.searchAreaManger(params);
		    if (list != null) {
			for (int i = 0; i < list.size(); i++) {
			    AreaMangerBack fb = list.get(i);
			    fb.setUuid(fb.getUuid());
			    fb.setAppState("2");
			    fb.setYwRoyaltyPay(fb.getYwRoyaltyPay());
			    fb.setDescribe(fb.getDescribe());
			    areaMangerService.updateAreaManger(fb);
			}
		    }
		}
	    }

	    balanceAppService.updateBalanceApp(model);
	    dwz.setMessage("审批成功");
	    dwz.setNavTabId("balanceAppList");
	    dwz.setStatusCode("200");
	    DwzAjaxResult.josnMessage(dwz);
	} catch (Exception e) {
	    e.printStackTrace();
	    dwz.setMessage("审批失败");
	    dwz.setStatusCode("300");
	    DwzAjaxResult.josnMessage(dwz);
	}

    }

    public void deleteForeign() {// 撤销申请单据
	DwzAjaxResult dwz = new DwzAjaxResult();
	Map<String, String> params = new HashMap<String, String>();
	params.put("appBillCode", foreign.getAppBillCode());
	List<ForeignBack> list = foreignService.searchForeign(params);
	int state = 2;
	try {
	    balanceAppService.deleteBalanceApp(model.getUuid());
	    state = 1;
	} catch (Exception e) {
	    e.printStackTrace();
	    state = 2;
	}
	if (list != null) {
	    for (int i = 0; i < list.size(); i++) {
		ForeignBack fb = list.get(i);
		try {
		    if (fb.getAppState().equals("1")) {
			fb.setUuid(fb.getUuid());
			fb.setAppBillCode("");
			fb.setAppState("0");
			fb.setYwCustomPay(0);
			fb.setYwSchoolPay(0);
			fb.setYwPhonePay(0);
			fb.setYwSysoperPay(0);
			foreignService.updateForeign(fb);
			state = 1;
		    }
		} catch (Exception e) {
		    state = 2;
		    e.printStackTrace();
		}
	    }
	}
	if (state == 1) {
	    dwz.setMessage("撤销成功");
	    dwz.setNavTabId("foreignList");
	    dwz.setStatusCode("200");
	    DwzAjaxResult.josnMessage(dwz);
	} else {
	    dwz.setMessage("撤销失败");
	    dwz.setStatusCode("300");
	    DwzAjaxResult.josnMessage(dwz);
	}
    }

    public void deleteAreaManger() {// 撤销申请单据
	DwzAjaxResult dwz = new DwzAjaxResult();
	int state = 2;
	Map<String, String> params = new HashMap<String, String>();
	params.put("appBillCode", areaManger.getAppBillCode());
	List<AreaMangerBack> list = areaMangerService.searchAreaManger(params);
	try {
	    balanceAppService.deleteBalanceApp(model.getUuid());
	    state = 1;
	} catch (Exception e) {
	    e.printStackTrace();
	    state = 2;
	}
	if (list != null) {
	    for (int i = 0; i < list.size(); i++) {
		AreaMangerBack fb = list.get(i);
		try {
		    if (fb.getAppState().equals("1")) {
			fb.setUuid(fb.getUuid());
			fb.setAppBillCode("");
			fb.setAppState("0");
			fb.setYwRoyaltyPay(0);
			fb.setDescribe("");
			areaMangerService.updateAreaManger(fb);
			state = 1;
		    }
		} catch (Exception e) {
		    state = 2;
		    e.printStackTrace();
		}
	    }
	}
	if (state == 1) {
	    dwz.setMessage("撤销成功");
	    dwz.setNavTabId("foreignList");
	    dwz.setStatusCode("200");
	    DwzAjaxResult.josnMessage(dwz);
	} else {
	    dwz.setMessage("撤销失败");
	    dwz.setStatusCode("300");
	    DwzAjaxResult.josnMessage(dwz);
	}
    }

    public void oneApproveBalanceAppPay() {
	DwzAjaxResult dwz = new DwzAjaxResult();
	String payDate = FormatDate.dateTimeDetail();
	payDate = payDate.substring(0, 12);
	String billState = model.getBillState();
	Map<String, String> params = new HashMap<String, String>();
	params.put("appBillCode", billCode);
	if (model.getAppType().equals("1")) {
	    List<ForeignBack> foreignList = foreignService.searchForeign(params);
	    if (foreignList != null) {
		for (int i = 0; i < foreignList.size(); i++) {
		    ForeignBack fb = foreignList.get(i);
		    fb.setUuid(fb.getUuid());
		    fb.setAppState("3");// 设置单据状态为签批
		    foreignService.updateForeign(fb);
		}
	    }
	} else {
	    List<AreaMangerBack> areaMangerList = areaMangerService.searchAreaManger(params);
	    if (areaMangerList != null) {
		for (int i = 0; i < areaMangerList.size(); i++) {
		    AreaMangerBack fb = areaMangerList.get(i);
		    fb.setUuid(fb.getUuid());
		    fb.setAppState("3");// 设置单据状态为签批
		    areaMangerService.updateAreaManger(fb);
		}
	    }
	}
	try {
	    model = balanceAppService.findBalanceAppById(model.getUuid());
	    model.setUuid(model.getUuid());
	    model.setPayDate(payDate);
	    model.setPayPerson(getCurrentUser().getUserId());
	    model.setBillState(billState);
	    balanceAppService.updateBalanceApp(model);
	    dwz.setMessage("签批成功");
	    dwz.setNavTabId("balanceAppPayList");
	    dwz.setStatusCode("200");
	    DwzAjaxResult.josnMessage(dwz);
	} catch (Exception e) {
	    e.printStackTrace();
	    dwz.setMessage("签批失败");
	    dwz.setStatusCode("300");
	    DwzAjaxResult.josnMessage(dwz);
	}
    }

    public String balanceAppList() {
	DwzAjaxResult dwz = new DwzAjaxResult();
	try {
	    QueryHelper qh = new QueryHelper(BalanceApp.class, "ba");
	    if (StringUtils.isNotEmpty(saleId)) {
		if (!PropertyValidate.validateUUID(saleId)) {
		    throw new Exception("输入不合法");
		}
		qh.addCondition("ba.appPerson =?", saleId);
	    }
	    qh.addCondition("ba.billState=?", "1");
	    qh.addOrderProperty("ba.appBillCode", false);
	    qh.preparePageBean(balanceAppService, pageNum, numPerPage);
	    // 准备销售部门
	    List<SysDepartment> deptList = new ArrayList<SysDepartment>();
	    if (getCurrentUser().getUserRole().getUsrDataRange() == 0) {// 全部数据权限，查询所有销售部门
		deptList = sysDepartmentService.findChildrenDepartment(SOPConfig.CODE_DEPT_SALE);
	    } else {// 部门数据权限或个人权限，查询本部门
		deptList.add(sysDepartmentService.getByCode(getCurrentUser().getDepartment().getDeptCode()));
	    }
	    ActionContext.getContext().put("deptList", deptList);
	    ActionContext.getContext().put("saleId", saleId);
	    return "balanceAppList";
	} catch (Exception e) {
	    dwz.setStatusCode("300");
	    dwz.setConfirmMsg("查询失败" + e.getMessage());
	    DwzAjaxResult.josnMessage(dwz);
	    return null;
	}
    }

    // 单据签批查看
    public String balanceAppPayList() {
	DwzAjaxResult dwz = new DwzAjaxResult();
	try{
	QueryHelper qh = new QueryHelper(BalanceApp.class, "ba");
	if (StringUtils.isNotEmpty(saleId)){
	    if (!PropertyValidate.validateUUID(saleId)) {
		throw new Exception("输入不合法");
	    }
	    qh.addCondition("ba.appPerson =?", saleId);  
	}
	qh.addCondition("ba.billState=?", "2");
	qh.addOrderProperty("ba.appBillCode", false);
	qh.preparePageBean(balanceAppService, pageNum, numPerPage);
	// 准备销售部门
	List<SysDepartment> deptList = new ArrayList<SysDepartment>();
	if (getCurrentUser().getUserRole().getUsrDataRange() == 0) {// 全部数据权限，查询所有销售部门
	    deptList = sysDepartmentService.findChildrenDepartment(SOPConfig.CODE_DEPT_SALE);
	} else {// 部门数据权限或个人权限，查询本部门
	    deptList.add(sysDepartmentService.getByCode(getCurrentUser().getDepartment().getDeptCode()));
	}
	ActionContext.getContext().put("deptList", deptList);
	ActionContext.getContext().put("saleId", saleId);
	return "balanceAppPayList";
	}catch(Exception e){
	    dwz.setStatusCode("300");
	    dwz.setConfirmMsg("单据查看失败"+e.getMessage());
	    DwzAjaxResult.josnMessage(dwz);
	    return null;
	}
    }

    // 单据已签批查看
    public String balanceAppPayManageList() {
	DwzAjaxResult dwz = new DwzAjaxResult();
	try{
	QueryHelper qh = new QueryHelper(BalanceApp.class, "ba");
	if (StringUtils.isNotEmpty(saleId)){
	    if (!PropertyValidate.validateUUID(saleId)) {
		throw new Exception("输入不合法");
	    } 
	    qh.addCondition("ba.appPerson =?", saleId);
	}
	qh.addCondition("ba.billState=?", "3");
	qh.addOrderProperty("ba.appBillCode", false);
	qh.preparePageBean(balanceAppService, pageNum, numPerPage);
	// 准备销售部门
	List<SysDepartment> deptList = new ArrayList<SysDepartment>();
	if (getCurrentUser().getUserRole().getUsrDataRange() == 0) {// 全部数据权限，查询所有销售部门
	    deptList = sysDepartmentService.findChildrenDepartment(SOPConfig.CODE_DEPT_SALE);
	} else {// 部门数据权限或个人权限，查询本部门
	    deptList.add(sysDepartmentService.getByCode(getCurrentUser().getDepartment().getDeptCode()));
	}
	ActionContext.getContext().put("deptList", deptList);
	ActionContext.getContext().put("saleId", saleId);
	return "balanceAppPayManageList";
	}catch(Exception e){
	    dwz.setStatusCode("300");
	    dwz.setConfirmMsg("单据签批查看失败"+e.getMessage());
	    DwzAjaxResult.josnMessage(dwz);
	    return null;
	}
    }

    public String appManageList() {// 单据查看
	DwzAjaxResult dwz = new DwzAjaxResult();
	try{
	QueryHelper qh = new QueryHelper(BalanceApp.class, "ba");
	if (StringUtils.isNotEmpty(saleId)){
	    if (!PropertyValidate.validateUUID(saleId)) {
		throw new Exception("输入不合法");
	    } 
	    qh.addCondition("ba.appPerson =?", saleId);
	}
	qh.addCondition("ba.billState=?", "2");
	qh.addOrderProperty("ba.appBillCode", false);
	qh.preparePageBean(balanceAppService, pageNum, numPerPage);
	// 准备销售部门
	List<SysDepartment> deptList = new ArrayList<SysDepartment>();
	if (getCurrentUser().getUserRole().getUsrDataRange() == 0) {// 全部数据权限，查询所有销售部门
	    deptList = sysDepartmentService.findChildrenDepartment(SOPConfig.CODE_DEPT_SALE);
	} else {// 部门数据权限或个人权限，查询本部门
	    deptList.add(sysDepartmentService.getByCode(getCurrentUser().getDepartment().getDeptCode()));
	}
	ActionContext.getContext().put("deptList", deptList);
	ActionContext.getContext().put("saleId", saleId);
	return "appManageList";
    }catch(Exception e){
	    dwz.setStatusCode("300");
	    dwz.setConfirmMsg("单据签批查看失败"+e.getMessage());
	    DwzAjaxResult.josnMessage(dwz);
	    return null;
	}
    }

    public String oldOrderList() {// 查询old的申请
	Map<String, String> params = new HashMap<String, String>();
	try {
	    orderUuid = new String(orderUuid.getBytes("ISO-8859-1"), "UTF-8");
	} catch (UnsupportedEncodingException e) {
	    e.printStackTrace();
	}
	if (orderUuid.equals("") || orderType.equals("")) {
	    System.out.println("查询旧单据异常!");
	} else if (orderType.equals("1")) {
	    params.put("hsSclid", orderUuid);
	    params.put("IsAppState", "1");
	    List<ForeignBack> list = foreignService.searchForeign(params);
	    ActionContext.getContext().put("orderList", list);
	} else if (orderType.equals("2")) {
	    params.put("areaCode", orderUuid);
	    params.put("IsAppState", "1");
	    List<AreaMangerBack> list = areaMangerService.searchAreaManger(params);
	    ActionContext.getContext().put("orderList", list);
	}
	ActionContext.getContext().put("orderType", orderType);
	return "oldOrderList";
    }

    public void findUser() {// 查找用户
	StringBuffer htmlcode = new StringBuffer();
	SysInsideUser insideUser = sysInsideUserService.getById(StringUtils.trim(saleId));
	ActionContext.getContext().getValueStack().push(insideUser);
	htmlcode.append(insideUser.getIsuName());
	HttpServletResponse response = ServletActionContext.getResponse();
	response.setHeader("Pragma", "No-Cache");
	response.setHeader("Cache-Control", "No-Cache");
	response.setDateHeader("Expires", 0);
	try {
	    response.setContentType("text/html");
	    response.setCharacterEncoding("utf-8");
	    response.getWriter().write(htmlcode.toString());
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }

    private ForeignBack foreign;
    private AreaMangerBack areaManger;
    private List<String> dataList;
    private String dataCost;
    private String billCode;
    private String saleId;
    private String orderType;
    private String orderUuid;

    public String getOrderType() {
	return orderType;
    }

    public void setOrderType(String orderType) {
	this.orderType = orderType;
    }

    public String getOrderUuid() {
	return orderUuid;
    }

    public void setOrderUuid(String orderUuid) {
	this.orderUuid = orderUuid;
    }

    public String getSaleId() {
	return saleId;
    }

    public void setSaleId(String saleId) {
	this.saleId = saleId;
    }

    public List<String> getDataList() {
	return dataList;
    }

    public void setDataList(List<String> dataList) {
	this.dataList = dataList;
    }

    public String getDataCost() {
	return dataCost;
    }

    public void setDataCost(String dataCost) {
	this.dataCost = dataCost;
    }

    public String getBillCode() {
	return billCode;
    }

    public void setBillCode(String billCode) {
	this.billCode = billCode;
    }

    public ForeignBack getForeign() {
	return foreign;
    }

    public void setForeign(ForeignBack foreign) {
	this.foreign = foreign;
    }

    public AreaMangerBack getAreaManger() {
	return areaManger;
    }

    public void setAreaManger(AreaMangerBack areaManger) {
	this.areaManger = areaManger;
    }

}
